import random

from faker import Faker

fake = Faker(locale="zh-CN")


def func_yaml(data):
    if isinstance(data, dict):
        for key, value in data.items():
            if '${' and '}' in str(value):
                start = str(value).index('${')
                end = str(value).index('}')
                func_name =str(value)[start + 2:end]
                # 如果yaml文件里name是  上海-${random_name()}形式，则写出data[key] = str(value)[0:start]+str(eval(func_name))
                data[key] = eval(func_name)
    return data


def random_name():
    return fake.name()

def age():
    return random.randint(0, 100)

if __name__ == '__main__':
    data = {'name': '${random_name()}', 'age': '${age()}',  'sex': '男'}
    print(func_yaml(data))
